# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re
import csv
from pymongo import MongoClient
import execjs


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


class Test_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        # requests.utils.dict_from_cookiejar(html.cookies)  # 获取cookies转dict
        # res.cookies.get_dict()  # 获取cookies
        self.cookie = cookiejar.CookieJar()
        # ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        ua = UserAgent(verify_ssl=False)  #
        self.headers = {
            # 'User-Agent': ua.random,
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'cache-control': 'no-cache',
            'Connection': 'keep-alive',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': 'JSESSIONID=D2DC2D8ACE2D3513ADC19B90F00DB56A.7; neCYtZEjo8Gm80S=2k3M5hhax5Q1L47_H8FgchkujLttYCLrCL92I9jreFEUZZiim9w.llhVrppG8O0I; neCYtZEjo8Gm80T=5cJCqAYiYHroTu7rfbFwbyTK5mQ5lXpKJoh2xhA6M3fFVraCgDxD5hsbE_wMZs7eYw6qrRsJdUNu9BEKywnjxjtB5uSNrrlyPnvGN34YAhKbq2H_5CFMkDaKQTH_.tBpM0yvdCMnEofIhp9nTBgyEkmmvqudmmaCFZ7lzLpr45_M9b_7TWT4vAxsm4XpTC4_GmwC4GNNeG8lLnWtKTunYkQ0Yh3l5VoTl3ZEXS1M2tMQtL6cIjQWbUfWOYSsBLkcdODT3nzHLz7zuMdPUzNFL8JrQEgEmDWL_pVltnVjIjJCUdVHokGivoGNn3cihY6pjl8xa1eJsduTDU9INR0Tz5vZHIgkC7ScU6u7Hq__DvTHonA.eynlC9.wjgkDcUhBLggg',
            'DNT': '1',
            'Host': 'app1.nmpa.gov.cn',
            'Origin': 'http://app1.nmpa.gov.cn',
            'Referer': 'http://app1.nmpa.gov.cn/datasearchcnda/face3/base.jsp?tableId=132&tableName=TABLE132&title=%D2%BD%C1%C6%C6%F7%D0%B5%C9%FA%B2%FA%C6%F3%D2%B5%A3%A8%D0%ED%BF%C9%A3%A9&bcId=154209313929078698414236686309&CbSlDlH0=LI_cqqqdHuZdHuZdHC1V5mY.U_JMplXSurSwPEw_KcaqqkZ',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'

        }


    def get_contents(self, cursor):
        postData = {
            'tableId': '26',
            'State': '1',
            'bcId': '152904417281669781044048234789',
            'State': '1',
            'curstart': '10',
            'State': '1',
            'tableName': 'TABLE26',
            'State': '1',
            'viewtitleName': 'COLUMN184',
            'State': '1',
            'viewsubTitleName': 'COLUMN181,COLUMN180',
            'State': '1',
            'tableView': '%E5%9B%BD%E4%BA%A7%E5%8C%BB%E7%96%97%E5%99%A8%E6%A2%B0%E4%BA%A7%E5%93%81%EF%BC%88%E6%B3%A8%E5%86%8C%EF%BC%89',
            'State': '1',
            'cid': '0',
            'State': '1',
            'ytableId': '0',
            'State': '1',
            'searchType': 'search',
            'State': '1'
        }

        url = f"http://app1.nmpa.gov.cn/datasearchcnda/face3/search.jsp?6SQk6G2z=GBK-5M4.vxOsnK6F1jFQDd7qrGkhRQjOO6vhj8egX1bjZrUou3D.IIdMR124BO.DMtFGnlrwbq2A3iIXkC5h8ll6XuZnRmvUAH1x3XM1JBVwnr6R.Vp9.5Clnarb8iowF71mOqe46JEf2TlYvHNIuVW5bsn59_k.cbwaL46_ML0pT963ZP33DDDaZDM4U.9446turb.KDuU_c2VAavV7fFwzOwdK3kzrdtIK8QlBJCo46r0NCdY5mhLe16m6g2CsGJzTcNiKhVewvtFsy14ld4l_DUeXDeYu2mC0_xkfkxI9MwJZYayJRbhOvh7kJiKGjeARqeKR31p5AyjKFzU5HUsNwh5W3yPoocp1qPbycoZOg2OhGdJP6zgJKWKaQ_Y.YX2DOuoZEBD0XhAbzwkYo6cDSgphsEuVPqb8_ARlP_CIVGBTfk0cvb6y.x7g.MKjgbGeAP0O"

        html = HttpUtils.do_request("POST", url, self.headers, postData)
        root = etree.HTML(html.text)
        for t in html.xpath('//tbody/tr/td[@height="30"]/p/a'):
            title = t.xpath('.//text()')[0].split(".")[1]

    # 解密sign
    def get_sing(self, params):
        with open("./decrypt.js", 'r', encoding="utf-8") as f:
            js_content = f.read()
            ctx = execjs.compile(js_content)
            # sign = ctx.call("u","appSignKey=4bTogwpz7RzNO2VTFtW7zcfRkAE97ox6ZSgcQi7FgYdqrHqKB7aGqEZ4o7yssa2aEXoV3bQwh12FFgVNlpyYk2Yjm9d2EZGeGu3&boardId=188&noncestr=16918870&orderType=2&pageNum=10&pageSize=30&postType=0&subBoardId=0&timestamp=1584761344473", "")
            sign = ctx.call("u1", "search.jsp", "")
        return sign


if __name__ == '__main__':
    test_get = Test_Get()
    # test_get.get_sing("")
    for i in range(1, 11):
        print(f"============第【{i}】页=============")
        test_get.get_contents(i)
